// PROGRAMA : SCRP1400
// SISTEMA : SISP - SISTEMA DE INFORMACOES DE SEGURANCA PUBLICA
// IDENTIFICACAO CRIMINAL
// BIBLIOTECA : SISP
// CLIENTE : GEJUSPC - GERENCIA DE ESTADO DA JUSTICA, SEGURANCA
// PUBLICA E CIDADANIA / INSTITUTO DE IDENT.
// OBJETIVO : CONSULTAS
// PROGRAMADOR: JOSE ANTONIO COELHO
// DATA : 11 DE SETEMBRO DE 1997
// ALTERACAO :
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrm1410;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp1400A;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp1400B;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrp1400C;
import br.com.vti.sisp_sicrim.antigo.model.SicCadastro;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Scrg001;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Scrp1400 OBJETIVO : CONSULTAS *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/scrp1400")
public class Scrp1400 {
	// Views utilizadas
	SicCadastro criminal = new SicCadastro();
	// Telas/relatorios utilizados
	Scrm1410 scrm1410;
	Scrp1400A scrp1400A;
	Scrp1400B scrp1400B;
	Scrp1400C scrp1400C;
	// Includes utilizados
	// Areas globais / session
	Scrg001 scrg001;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();
	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Scrp1400 scrp1400Anterior;
	String faseproc = "0";

	// private String[] jOpc = new String[5];
	private int jDataIni = 0;
	private int jDataFim = 0;
	private int jData = UtilBean.obtemDataCorrente();
	private String mensagemInicial = "";

	private Scrm1410 preMapas() {
		int ind;
		scrm1410 = new Scrm1410(true, sessao);
		scrp1400A = new Scrp1400A(true, sessao);
		scrp1400B = new Scrp1400B(true, sessao);
		scrp1400C = new Scrp1400C(true, sessao);

		scrm1410.setDataCorrente(UtilBean.obtemDataDia());
		for (ind = 1; ind <= 4; ind++) {
			scrm1410.setJopc(ind, "");
		}

		sessao.getScrg001().setWVltw(9);
		sessao.getScrg001().setWFuncaow("CONSULTAS");
		sessao.getScrg001().setWProgramaw(this.getClass().getSimpleName());
		return scrm1410;
	}

	public Scrp1400(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(Scrm1410 scrm1410, Scrp1400A scrp1400A, Scrp1400B scrp1400B, Scrp1400C scrp1400C) {
		try {
			main(scrm1410, scrp1400A, scrp1400B, scrp1400C);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroScrcerro(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main(Scrm1410 pscrm1410, Scrp1400A pscrp1400A, Scrp1400B pscrp1400B, Scrp1400C pscrp1400C) {
		if (pscrm1410 == null && pscrp1400A == null && pscrp1400B == null && pscrp1400C == null) {
			primeiraVez = true;
			// scrm1410 = new Scrm1410 (primeiraVez);
			scrm1410 = preMapas();
			scrm1410.setNavegar("../scrp1400/executar");
			scrm1410.setMensagem(mensagemInicial);
			result.include("scrm1410", scrm1410);
			sessao.setAtributo("SCRP1400", this);
			scrm1410.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return;
		} else {
			restaurarEstados(pscrm1410, pscrp1400A, pscrp1400B, pscrp1400C);
			if (pscrp1400A != null) {
				processarTelaDatas();
				return;
			}
		}
		jData = UtilBean.obtemDataCorrente();

		scrm1410.receberTela(false, result);
		switch (scrm1410.getLincol()) {
		case 11044: {
			if (scrp1400B == null) {
				result.include("scrp1400B", scrp1400B);
				sessao.setAtributo("SCRP1400", this);
				scrp1400B.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (!verificaData())
				return;
			Scrp141a scrp141a = new Scrp141a(result, sessao);
			scrp141a.jDataIni = jDataIni;
			scrp141a.executar(null);
			return;
		}
		// FETCH 'SCRP1410' #DATA-INI
		case 13047: {
			Scrp142a scrp142a = new Scrp142a(result, sessao);
			scrp142a.executar(null);
			return;
		}
		// FETCH 'SCRP1420'
		case 15050: {
			if (scrp1400B == null) {
				result.include("scrp1400B", scrp1400B);
				sessao.setAtributo("SCRP1400", this);
				scrp1400B.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return;
			}
			if (!verificaData())
				return;
			Scrp1430 scrp1430 = new Scrp1430(result, sessao);
			// scrpt1430.jDataIni = jDataIni;
			scrp1430.executar(null, jDataIni);
			return;
		}
		case 17053: {

			result.include("scrp1400A", scrp1400A);
			sessao.setAtributo("SCRP1400", this);
			scrp1400A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		default: {
			break;
		}
		}
		sessao.setAtributo("SCRP1400", this);
		result.include("scrm1410", scrm1410);
		scrm1410.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
	}

	private boolean verificaData() {

		scrp1400B.receberTela(false, result);
		if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
			scrp1400B.enviarMensagem(null, "Tecla invalida");
			return true;
		}
		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			Scrp1400 scrp1400 = new Scrp1400(result, sessao);
			scrp1400.executar(null, null, null, null);
		}
		if (scrp1400B.getjdiaIni() == 0 || scrp1400B.getjmesIni() == 0 || scrp1400B.getjanoIni() == 0) {
			scrp1400B.enviarMensagem("jdiaini", "Data Inicial invalida");
			return true;
		}
		if (scrp1400B.getjanoIni() < 1997) {
			scrp1400B.enviarMensagem("janoini", "Ano invalido");
			return true;
		}
		if (scrp1400B.getWdataIni() > jData) {
			scrp1400B.enviarMensagem("jdiaini", "Data invalida");
			return true;
		}
		// Par�grafo: fd1()
		criminal.consultaDataDocAtuStatusRegDifer(jDataIni, 5);
		//

		if (scrp1400C == null) {
			result.include("scrp1400C", scrp1400C);
			sessao.setAtributo("SCRP1400", this);
			scrp1400C.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return true;
		}
		scrp1400C.receberTela(false, result);
		if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
			scrp1400C.enviarMensagem(null, "Tecla invalida");
			return true;
		}
		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			Scrp1400 scrp1400 = new Scrp1400(result, sessao);
			scrp1400.executar(null, null, null, null);
			return false;
		}
		return true;
	}

	private void restaurarEstados(Scrm1410 pscrm1410, Scrp1400A pscrp1400A, Scrp1400B pscrp1400B, Scrp1400C pscrp1400C) {
		this.scrp1400Anterior = (Scrp1400) sessao.getAtributo("SCRP1400");
		if (pscrm1410 != null) {
			scrm1410 = pscrm1410;
		} else {
			scrm1410 = this.scrp1400Anterior.scrm1410;
		}
		scrm1410.setSessao(sessao);
		if (pscrp1400A != null) {
			scrp1400A = pscrp1400A;
		} else {
			scrp1400A = this.scrp1400Anterior.scrp1400A;
		}
		scrp1400A.setSessao(sessao);
		if (pscrp1400B != null) {
			scrp1400B = pscrp1400B;
		} else {
			scrp1400B = this.scrp1400Anterior.scrp1400B;
		}
		scrp1400B.setSessao(sessao);
		if (pscrp1400C != null) {
			scrp1400C = pscrp1400C;
		} else {
			scrp1400C = this.scrp1400Anterior.scrp1400C;
		}
		scrp1400C.setSessao(sessao);
		this.faseproc = this.scrp1400Anterior.faseproc;
	}

	protected void setMensagem(String mensagem) {
		mensagemInicial = mensagem;
	}

	private void processarTelaDatas() {
		scrp1400A.receberTela(false, result);
		if (!((UtilBean.funcao.equals(UtilBean.ctt_retorna) || UtilBean.funcao.equals(UtilBean.ctt_processa)))) {
			scrp1400A.enviarMensagem(null, "Tecla invalida");
			result.include("scrp1400A", scrp1400A);
			sessao.setAtributo("SCRP1400", this);
			scrp1400A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			Scrp1400 scrp1400 = new Scrp1400(result, sessao);
			scrp1400.executar(null, null, null, null);
			return;
		}
		jDataIni = scrp1400A.getWdataIni();
		jDataFim = scrp1400A.getWdataFim();
		if (scrp1400A.getjdiaIni() == 0 || scrp1400A.getjmesIni() == 0 || scrp1400B.getjanoIni() == 0) {
			scrp1400A.enviarMensagem("jdiaini", "Data Inicial invalida");
			result.include("scrp1400A", scrp1400A);
			sessao.setAtributo("SCRP1400", this);
			scrp1400A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (scrp1400A.getjanoIni() < 1997) {
			scrp1400A.enviarMensagem("janoini", "Ano invalido");
			result.include("scrp1400A", scrp1400A);
			sessao.setAtributo("SCRP1400", this);
			scrp1400A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (jDataIni > jData) {
			scrp1400A.enviarMensagem("jdiaini", "Data invalida");
			result.include("scrp1400A", scrp1400A);
			sessao.setAtributo("SCRP1400", this);
			scrp1400A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (scrp1400A.getjdiaFim() == 0 || scrp1400A.getjmesFim() == 0 || scrp1400A.getjanoFim() == 0) {
			scrp1400A.enviarMensagem("jdiafim", "Data Final invalida");
			result.include("scrp1400A", scrp1400A);
			sessao.setAtributo("SCRP1400", this);
			scrp1400A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (scrp1400A.getjanoFim() < 1997) {
			scrp1400A.enviarMensagem("janofim", "Ano invalido");
			result.include("scrp1400A", scrp1400A);
			sessao.setAtributo("SCRP1400", this);
			scrp1400A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (jDataFim < jDataIni) {
			scrp1400A.enviarMensagem("jdiafim", "Data final invalida");
			result.include("scrp1400A", scrp1400A);
			sessao.setAtributo("SCRP1400", this);
			scrp1400A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		if (jDataFim > jData) {
			scrp1400A.enviarMensagem("jDataFim", "Data final invalida");
			result.include("scrp1400A", scrp1400A);
			sessao.setAtributo("SCRP1400", this);
			scrp1400A.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return;
		}
		Scrp1440 scrp1440 = new Scrp1440(result, sessao);
		scrp1440.executar(null, null, jDataIni, jDataFim);
	}
}
